package org.deltaset.secured.visit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.deltaset.secured.user.Common;
import org.deltaset.secured.user.EmbeddedFile;

@Entity
public class Person implements Serializable {
    
    private static final Logger logger = Logger.getLogger(Person.class.getName());

    //@GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Id
    //@GeneratedValue(generator = "system-uuid")
    //@Column(length = 36)
    //@Column(columnDefinition="UUID")
    //private String id= UUID.randomUUID().toString();
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Version
    @Column(name = "version", nullable = false, updatable = false, unique = false)
    private Long version;
    @NotNull
    @Enumerated(EnumType.STRING)
    private NameTitle title;
    @NotNull
    private String firstName;
    @NotNull
    @Size(min = 3, max = 10)
    private String familyName;
    //@NotNull
    private int smInt;
    private boolean smBoolean;
    private Boolean bBoolean;
    @Temporal(TemporalType.DATE)
    private Date today;
    @OneToMany(mappedBy = "owner", fetch = FetchType.EAGER, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    private List<Animal> animals;
    private Common common;
    
    private EmbeddedFile uploadedFile;        

    public EmbeddedFile getUploadedFile() {
        if (null == uploadedFile) {
            uploadedFile = new EmbeddedFile();
        }
        return uploadedFile;
    }

    public void setUploadedFile(EmbeddedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public Common getCommon() {
        if (null == common) {
            common = new Common();
        }
        return common;
    }

    public void setCommon(Common common) {
        this.common = common;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long id) {
        this.version = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    public String title() {
        final StringBuilder sb = new StringBuilder();
        sb.append(getFamilyName().toUpperCase());
        sb.append(", ");
        sb.append(getFirstName());
        return sb.toString();
    }

    public int getSmInt() {
        return smInt;
    }

    public void setSmInt(int smInt) {
        this.smInt = smInt;
    }

    public boolean isSmBoolean() {
        return smBoolean;
    }

    public void setSmBoolean(boolean smBoolean) {
        this.smBoolean = smBoolean;
    }

    public Boolean getbBoolean() {
        return bBoolean;
    }

    public void setbBoolean(Boolean bBoolean) {
        this.bBoolean = bBoolean;
    }

    public Date getToday() {
        return today;
    }

    public void setToday(Date today) {
        this.today = today;
    }

    public NameTitle getTitle() {
        return title;
    }

    public void setTitle(NameTitle title) {
        this.title = title;
    }

    public List<Animal> getAnimals() {
        if (null == animals) {
            animals = new ArrayList<Animal>();
        }
        return animals;
    }

    public Person addAnimal(final AnimalDto animalDto) {
        final Animal animal = animalDto.getAnimal();
        animal.setOwner(this);
        this.getAnimals().add(animal);
        return this;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if (this.version != other.version && (this.version == null || !this.version.equals(other.version))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 89 * hash + (this.version != null ? this.version.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", version=" + version + ", title=" + title + ", firstName=" + firstName + ", familyName=" + familyName + ", uploadedFile=" + uploadedFile + '}';
    }

    @PrePersist
    private void showPrePersist() {       
        logger.log(Level.INFO, " PrePersist person={0}", this.toString());
    }

    @PreUpdate
    private void showPreUpdate() {       
        logger.log(Level.INFO, " PreUpdate person={0}", this.toString());
    }
}
